package com.ustcinfo.study.mr.pq;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 统计访问日志中每天的访问量与每小时平均访问量
 * @author pengqaing
 */
public class DayStatistical {
    private static class MyMapper extends Mapper<Object, Text, Text, IntWritable>{
        private static final IntWritable count = new IntWritable(1);
        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            Pattern pattern = Pattern.compile("\\[(.*?):"); //日期提取正则表达式
            Matcher matcher = pattern.matcher(line);
            if(matcher.find()){
                if (matcher.group(1).length()>0)  //判断key日期是否为空字符串
                context.write(new Text(matcher.group(1)),count); //以日期为key，如27/Feb/2017为一个key，28/Feb/2017为一个key
            }
            }
        }
    private static class MyCombiner extends Reducer<Text,IntWritable,Text,IntWritable>{ //重编写combiner，达到优化MR的目的
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for(IntWritable val :values){
                sum += val.get();          //局部统计，减少map输出
            }
            context.write(key,new IntWritable(sum));
        }
    }
    private static class MyReduce extends Reducer<Text, IntWritable, Text, Text>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            int avg = 0;
            for(IntWritable val :values){
                sum += val.get();             //根据日期总加和
            }
            if(sum > 0){
                avg = sum / 24;  //每天总量除24获得平均小时访问
            }
            context.write(key,new Text("总访问:"+sum+",平均每小时访问量:"+avg)); //reduce输出
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance();
        job.setJobName("pq_day_Statistical");
        job.setJarByClass(DayStatistical.class);

        job.setMapperClass(MyMapper.class);  //map类
        job.setCombinerClass(MyCombiner.class);  //combiner类
        job.setReducerClass(MyReduce.class);   //reduce类

        job.setMapOutputKeyClass(Text.class);   //取消map默认输出格式
        job.setMapOutputValueClass(IntWritable.class);  //自定义map输出格式

        job.setOutputKeyClass(Text.class);  //reduce输出格式
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job,new Path(args[0]));  //输入文件路径
        FileOutputFormat.setOutputPath(job,new Path(args[1]));  //输出文件路径

        int status = job.waitForCompletion(true) ? 0 : 1;
        System.exit(status);
    }
}